package org.example.chatservice.broker;

import lombok.RequiredArgsConstructor;
import org.example.chatservice.entity.ChatMessage;
import org.example.chatservice.filter.SensitiveWordFilter;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MessageBroker {
    private final RabbitTemplate rabbitTemplate;
    private final SensitiveWordFilter sensitiveWordFilter;

    private static final String CHAT_EXCHANGE = "chat.exchange";
    private static final String PRIVATE_ROUTING_KEY = "chat.private.";
    private static final String GROUP_ROUTING_KEY = "chat.group.";

    public void publishPrivateMessage(ChatMessage message) {
        // 过滤敏感词
        message.setContent(sensitiveWordFilter.filter(message.getContent()));
        
        // 发布到私聊队列
        rabbitTemplate.convertAndSend(
            CHAT_EXCHANGE,
            PRIVATE_ROUTING_KEY + message.getReceiverId(),
            message
        );
    }

    public void publishGroupMessage(ChatMessage message) {
        // 过滤敏感词
        message.setContent(sensitiveWordFilter.filter(message.getContent()));
        
        // 发布到群聊队列
        rabbitTemplate.convertAndSend(
            CHAT_EXCHANGE,
            GROUP_ROUTING_KEY + message.getReceiverId(),
            message
        );
    }
}